package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sangeng.entity.LoginUserDetailsImpl;
import com.sangeng.entity.User;
import com.sangeng.mapper.LoginUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;
//一定记得标Service
@Service
public class UserDetailServiceImpl implements UserDetailsService {
/**实现改接口，userServiceImpl里面authenticationManager.authenticate(usernamePasswordAuthenticationToken);
 * 会调用到该方法
    **/
@Autowired
LoginUserMapper loginUserMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUserName,s);
        User user = loginUserMapper.selectOne(userLambdaQueryWrapper);
        if(Objects.isNull(user)){
            new RuntimeException("用户不存在");
        }
        //返回用户信息
        //TODO 查询权限信息封装

        //因为返回值是UserDetails，所以得将查询到的信息封装成UserDetails或者UserDetails的实现类loginUserDetailsImpl
        LoginUserDetailsImpl loginUserDetails = new LoginUserDetailsImpl(user);

        return loginUserDetails;
    }
}
